package hrhq.cntvcn.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import hrhq.cntvcn.pojo.HrhqVideo;
import hrhq.cntvcn.pojo.SearchResult;
import hrhq.cntvcn.service.SearchVideoService;

@Controller
@RequestMapping({ "/search" })
public class SearchVideoController {

	private static Logger logger = Logger.getLogger(SearchVideoController.class);

	/**
	 * 拦截首页访问 跳转测试方法
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	@RequestMapping({ "/video/index" })
	public void index(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		resp.sendRedirect("http://localhost:8081/search/Video/byTitle");
	}

	@Autowired
	private SearchVideoService svService;

	/**
	 * 根据文章标题关键词进行搜索
	 * 
	 * @param titleKey
	 *            用户输入的文章标题关键词
	 * @param rows
	 *            每页展示的结果数
	 * @param currPage
	 *            当前页码
	 * @param model
	 * @return searchTest 搜索显示页面
	 * @throws IOException 
	 */
	@RequestMapping({ "Video/byTitle" })
	public String searchByVideoTitle(String titleKey, @RequestParam(defaultValue = "20") Integer rows,
			@RequestParam(defaultValue = "1") Integer currPage, Model model)  {
		long start = System.currentTimeMillis();
		try {
			SearchResult<HrhqVideo> searchResult = svService.searchByVideoTitle("决斗", rows, currPage);
			model.addAttribute("searchResult", searchResult);
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println("搜索用时："+(end - start));
		return "searchTestVideo";
	}
}
